﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 养老金支付系统
{
    public partial class FormJYSBCX : FormBase
    {
        public FormJYSBCX()
        {
            InitializeComponent();
        }

        string zfqh;
        string dfyh;
        string yhmc;
        string zflx;

        const string sql = @"select 支付期号,代发银行名称 as 代发银行,支付类型,个人编号,姓名,身份证号,支付金额,银行名称,银行帐号,交易状态,失败原因 
from 查支付明细 where 支付期号=@ZFQH and 支付类型 like '%'+@ZFLX+'%'and 银行名称 like '%'+@YHMC+'%'and 代发银行名称 like '%'+@DFYH+'%'
and 交易状态码=0 and 支付期号<=dbo.GetMaxZZQH() order by 代发银行编号,银行编号,失败原因码,支付类型码";

        SaveFileDialog sfd = new SaveFileDialog()
        {
            Title = "导出",
            Filter = "Excel(97-2003)|*.xls"
        };

        void buttonEnabled(bool b)
        {
            button1.Enabled = b;
            button2.Enabled = b;
            button3.Enabled = b;
        }

        void init()
        {
            this.Text = "支付明细查询";
            this.zfqh = SqlHelper.getScalar("select dbo.GetMaxZZQH()") as string;
            this.dfyh = "";
            this.yhmc = "";
            this.zflx = "";
            textBox1.Text = this.zfqh;
            comboBox1.Items.Add("");
            comboBox1.Items.AddRange(SqlHelper.getFirstColumn("select 银行名称 from 支付银行 where 银行编号 in (select 代发银行编号 from 支付银行)"));
            comboBox2.Items.Add("");
            comboBox2.Items.AddRange(SqlHelper.getFirstColumn("select 银行名称 from 支付银行"));
            comboBox3.Items.Add("");
            comboBox3.Items.AddRange(SqlHelper.getFirstColumn("select 支付类型 from 支付类型"));
        }


        void find()
        {
            if (this.zfqh.Length == 6)
            {
                SqlParameter[] p = new SqlParameter[] { 
                    new SqlParameter("@ZFQH",this.zfqh),
                    new SqlParameter("@DFYH",this.dfyh),
                    new SqlParameter("@YHMC",this.yhmc),
                    new SqlParameter("@ZFLX",this.zflx)
                };
                dataGridView1.DataSource = SqlHelper.getDataTable(sql, p);
            }
        }

        void output()
        {
            DataTable dt = dataGridView1.DataSource as DataTable;
            if (dt != null)
            {
                sfd.FileName = "第" + this.zfqh + "期" + this.yhmc + "交易失败明细.xls";
                if (sfd.ShowDialog() == DialogResult.OK)
                    ExcelHelper.DataTableToExcel(dt, sfd.FileName);
            }
        }

        void print()
        {
            DataTable dt = dataGridView1.DataSource as DataTable;
            if (dt != null)
            {
                FormPrintView fpv = new FormPrintView()
                {
                    ReportDataSetName = "支付明细",
                    DataSources = dataGridView1.DataSource,
                    ReportParam = new Microsoft.Reporting.WinForms.ReportParameter[] { 
                        new Microsoft.Reporting.WinForms.ReportParameter("qh", this.zfqh), 
                        new Microsoft.Reporting.WinForms.ReportParameter("yh",this.yhmc) 
                    },
                    ReportPath = "JYSBMX.rdlc"
                };
                fpv.ShowDialog();
            }
        }

        private void FormJYSBCX_Load(object sender, EventArgs e)
        {
            init();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            buttonEnabled(false);
            find();
            buttonEnabled(true);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            buttonEnabled(false);
            output();
            buttonEnabled(true);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            this.zfqh = textBox1.Text;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.dfyh = (sender as ComboBox).Text;
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.yhmc = (sender as ComboBox).Text;
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.zflx = (sender as ComboBox).Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            buttonEnabled(false);
            print();
            buttonEnabled(true);
        }
    }
}
